package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.log.MyLog;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody Map<String,String> map, HttpSession session){
        //获取手机号
        String phone = map.get("phone");
        //判断redis中存在不
        if (redisTemplate.opsForValue().get(phone) != null) {
            return R.success("验证码还有效,请勿重复发送");
        }
        //1.随机生成验证码
//        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        String code = "1234";
        log.info("开始发送验证码:{}",code);
        //2.调用阿里巴巴发送验证码
//        SMSUtils.sendMessage("ABC商城", "SMS_208640979", phone, code);
        //3.将验证码存入redis
//        session.setAttribute(phone,code);
        //需要将生成的验证码保存到Redis,设置过期时间
        redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
        //4.返回发送验证码成功
        return R.success("验证码发送成功");
    }

    @PostMapping("/login")
//    @MyLog("用户登录")
    public R<String> login(@RequestBody Map<String,String> map,HttpSession session){
        //1.获取用户输入的用户名和验证码
        String phone = map.get("phone");
        String code = map.get("code");
        //2.获取redis中的验证码
        //String codeInSession = (String) session.getAttribute(phone);
        String codeInRedis = (String) redisTemplate.opsForValue().get(phone);

        //3.比对验证码
        if (StringUtils.isEmpty(codeInRedis) || !codeInRedis.equals(code)){
            //3.1验证码不一致,提示错误信息
            return R.error("验证码错误");
        }
        //4.判断用户是否为新用户,根据数据库查询
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,phone);
        User user = userService.getOne(lqw);
        if (user == null) {
            //4.1是新用户,然后添加到数据库
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }else{
            //4.2老用户,判断status状态,0为禁用状态,给出错误提示,    1为启用状态,直接登陆
            Integer status = user.getStatus();
            if (status == 0) {
                //禁用
                return R.error("用户已禁用");
            }
        }
        //5.登陆成功后,将验证码从redis中移除
        //session.removeAttribute(phone);
        redisTemplate.delete(phone);
        //6.将用户id放入session
        session.setAttribute("user",user.getId());
        return R.success("登录成功");

    }

    @PostMapping("/loginout")
//    @MyLog("用户退出")
    public R<String> loginout(HttpSession session){
        //清理session中保存的当前用户的登录信息
        session.removeAttribute("user");
        return R.success("退出登录");



    }

}